/*
 *   Copyright (c) 2015, Andrew Romanenko <melanhit@gmail.com>
 *   All rights reserved.
 *
 *   Redistribution and use in source and binary forms, with or without
 *   modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright notice, this
 *      list of conditions and the following disclaimer.
 *   2. Redistributions in binary form must reproduce the above copyright notice,
 *      this list of conditions and the following disclaimer in the documentation
 *      and/or other materials provided with the distribution.
 *   3. Neither the name of the project nor the names of its contributors
 *      may be used to endorse or promote products derived from this software
 *      without specific prior written permission.
 *
 *   THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
 *   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 *   ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdlib.h>
#include <stdint.h>
#include <string.h>

#include "../akmos.h"
#include "../cipher.h"

#include "ctr.h"

int akmos_ctr_init(akmos_cipher_t *ctx)
{
    akmos_ctr_t *ptr;

    ptr = (akmos_ctr_t *)ctx->mctx;

    ptr->buf = malloc(ctx->algo->blklen * 2);
    if(!ptr->buf)
        return AKMOS_ERR_ENOMEM;

    return AKMOS_ERR_SUCCESS;
}

void akmos_ctr_setiv(akmos_cipher_t *ctx, const uint8_t *iv)
{
    akmos_ctr_t *ptr;
    size_t len;

    len = ctx->algo->blklen - sizeof(uint64_t);

    ptr = (akmos_ctr_t *)ctx->mctx;

    ptr->iv  = ptr->buf;
    ptr->tmp = ptr->buf + ctx->algo->blklen;
    ptr->cnt = (uint64_t *)(ptr->iv + len);

    if(!iv)
        memset(ptr->iv, 0, len);
    else
        memcpy(ptr->iv, iv, len);

    memset(ptr->cnt, 0, sizeof(uint64_t));
}

void akmos_ctr_encrypt(akmos_cipher_t *ctx, const uint8_t *in_blk, size_t in_len, uint8_t *out_blk)
{
    akmos_ctr_t *ptr;
    size_t i, j, n, y, blklen;
    uint32_t *p_in, *p_out, *p_tmp;
    uint64_t cnt;

    ptr = (akmos_ctr_t *)ctx->mctx;
    cnt = *ptr->cnt;

    blklen = ctx->algo->blklen;
    n = in_len / blklen;
    y = blklen / sizeof(uint32_t);

    p_in  = (uint32_t *)in_blk;
    p_out = (uint32_t *)out_blk;
    p_tmp = (uint32_t *)ptr->tmp;

    for(i = 0; i < n; i++) {
        ctx->algo->encrypt(ctx->actx, ptr->iv, ptr->tmp);

        for(j = 0; j < y; j++, p_in++, p_out++)
            *p_out = *p_in ^ *p_tmp;

        out_blk += blklen;
        in_blk  += blklen;
        cnt++;
    }

    n = in_len - (n * blklen);
    ctx->algo->encrypt(ctx->actx, ptr->iv, ptr->tmp);

    for(j = 0; j < n; j++, out_blk++, in_blk++)
        *out_blk = *in_blk ^ ptr->tmp[j];

    cnt++;
    *ptr->cnt = cnt;
}

void akmos_ctr_free(akmos_cipher_t *ctx)
{
    akmos_ctr_t *ptr;

    if(!ctx)
        return;

    ptr = (akmos_ctr_t *)ctx->mctx;

    if(ptr->buf) {
        memset(ptr->buf, 0, ctx->algo->blklen * 2);
        free(ptr->buf);
    }

    free(ptr);
}
